package org.example.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.Interceptors.adminInterceptor;
import org.example.Interceptors.assetInterceptor;
import org.example.Interceptors.loginInterceptor;
import org.example.Interceptors.testerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description config for web mvc: message converter, view resolver, template resolver, etc.
 * @Author 魏锴
 * @Date 2023/11/17
 */
@Configuration
@ComponentScan(basePackages = "org.example")
@EnableWebMvc
@PropertySource(value = "classpath:avatar.properties", encoding = "utf-8")
public class WebMVCConfig implements WebMvcConfigurer {
    // 当前跨域请求最大有效时长。这里默认1天
    private static final long MAX_AGE = 16 * 60;
    @Autowired
    private ApplicationContext applicationContext;
    @Value("${upload_url}")
    public String UPLOAD_URL;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        //设置日期格式
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        objectMapper.setDateFormat(smt);
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //设置中文编码格式
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON_UTF8);
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
        messageConverters.add(0, mappingJackson2HttpMessageConverter);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(MAX_AGE)
                .allowedHeaders("token", "host", "connection", "accept", "access-control-request-method",
                        "access-control-request-headers", "origin", "user-agent", "sec-fetch-mode",
                        "sec-fetch-site", "sec-fetch-dest", "referer", "accept-encoding", "accept-language");
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/index/login");//登录
//                .excludePathPatterns("/avatars/**")
//                .excludePathPatterns("/static/avatars");
        //添加用户验证
        registry.addInterceptor(new assetInterceptor())
                .addPathPatterns("/asset/**");
        registry.addInterceptor(new testerInterceptor())
                .addPathPatterns("/tester/**");
        registry.addInterceptor(new adminInterceptor())
                .addPathPatterns("/admin/**");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/avatars/**").addResourceLocations("/static/avatars");
    }

    @Bean
    public MultipartResolver multipartResolver() throws IOException {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(104857600);
        return multipartResolver;
    }
}
